<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
        <p:panelGrid styleClass="organisationGrid" columns="2">
            <p:panel id="displayArticles" header="Articles list">
                <h:form id="selectArticleStateForm">
                    <p:selectOneRadio value="#{myArticleBean.selectedState}">
                        <f:selectItem itemLabel="All" itemValue="All"/>
                        <f:selectItems value="#{myArticleBean.articleState}" />
                        <p:ajax event="change" update=":dataArticle" />
                    </p:selectOneRadio>
                </h:form>
                <p:dataTable id="dataArticle" var="article" value="#{myArticleBean.listArticle}"
                             styleClass="standardDataTable">
                    <p:column headerText="Name">
                        <h:outputText value="#{article.name}"/>
                    </p:column>
                    <p:column headerText="Auction's End">
                        <h:outputText value="#{article.endSale}">
                            <f:convertDateTime pattern="dd/MM/yyyy HH:mm" />
                        </h:outputText>
                    </p:column>
                    <p:column headerText="Last Auction">
                        <h:outputText value="#{article.actualPrice}">
                            <f:convertNumber type="currency" currencySymbol="€" />
                        </h:outputText>
                    </p:column>
                    <p:column headerText="Status">
                        <h:outputText value="#{article.articleState}" />
                    </p:column>
                    <p:column headerText="Details"> 
                        <h:form id="viewDetailsForm">
                            <p:commandLink id="selectButton" update=":displayArticle" action="#{myArticleBean.articleSelected}">  
                                <img src="resources/images/ic_show.png" />
                                <f:setPropertyActionListener value="#{article}" target="#{myArticleBean.selectedArticle}" />
                                <p:ajax event="click" />
                            </p:commandLink>
                        </h:form>
                    </p:column>
                </p:dataTable>                   
            </p:panel>

            <p:panel id="displayArticle" header="Details" >
                <h:form>
                    <p:panelGrid styleClass="organisationGrid" columns="2">
                        <h:outputText value="Name" styleClass="outputTextLabel"/>
                        <h:outputText value="#{myArticleBean.selectedArticle.name}" styleClass="outputTextValue" 
                                      rendered="#{!(myArticleBean.selectedArticle.articleState == 'canceled')}"/>
                        <p:inplace rendered="#{myArticleBean.selectedArticle.articleState == 'canceled'}" editor="true">
                            <p:inputText label="Name" value="#{myArticleBean.selectedArticle.name}" styleClass="outputTextValue" required="true" />
                            <p:ajax event="save" listener="#{myArticleBean.updateArticle}" update=":displayArticles,:headerForm:growl"/>
                        </p:inplace>
                        <h:outputText value="Status" styleClass="outputTextLabel" />
                        <h:outputText value="#{myArticleBean.selectedArticle.articleState}" styleClass="outputTextValue"/>
                        <h:outputText value="Description" styleClass="outputTextLabel" />
                        <h:outputText value="#{myArticleBean.selectedArticle.description}" styleClass="outputTextValue"
                                      rendered="#{!(myArticleBean.selectedArticle.articleState == 'canceled')}"/>
                        <p:inplace  editor="true" rendered="#{myArticleBean.selectedArticle.articleState == 'canceled'}">
                            <p:inputText id="descEdit" label="Description" maxlength="255" value="#{myArticleBean.selectedArticle.description}" styleClass="outputTextValue" required="true" />
                            <p:ajax event="save" listener="#{myArticleBean.updateArticle}" update=":displayArticles,:headerForm:growl"/>
                        </p:inplace>
                        <h:outputText value="ActualPrice" styleClass="outputTextLabel"/>
                        <h:outputText rendered="#{!(myArticleBean.selectedArticle.articleState == 'canceled')}" value="#{myArticleBean.selectedArticle.actualPrice}" styleClass="outputTextValue">
                            <f:convertNumber type="currency" />
                        </h:outputText>
                        <p:inplace rendered="#{myArticleBean.selectedArticle.articleState == 'canceled'}" editor="true">
                            <p:inputText label="Price" maxlength="255" value="#{myArticleBean.selectedArticle.startPrice}" styleClass="outputTextValue" required="true" >
                                <f:validateDoubleRange minimum="1" />
                                <f:convertNumber  type="currency"/>
                            </p:inputText>
                            <p:ajax event="save" listener="#{myArticleBean.updateArticlePrice()}" update=":displayArticles,:headerForm:growl"/>
                        </p:inplace>
                        <h:outputText value="Categories" styleClass="outputTextLabel" />
                        <p:dataList rendered="#{!(myArticleBean.selectedArticle.articleState == 'canceled')}" 
                                    value="#{myArticleBean.selectedArticle.categories}" var="cat" >
                            <h:outputText value="#{cat}" styleClass="outputTextValue" style="margin-right: 10px"/>
                        </p:dataList>
                        <p:inplace rendered="#{myArticleBean.selectedArticle.articleState == 'canceled'}" editor="true">
                            <p:selectCheckboxMenu label="Categories" value="#{myArticleBean.selectedCategories}" required="true">
                                <f:selectItems value="#{submitArticleBean.categoriesValue}"/>
                            </p:selectCheckboxMenu>
                            <p:ajax event="save" listener="#{myArticleBean.updateArticleCategories()}" update=":displayArticles,:headerForm:growl"/>
                        </p:inplace>
                        <p:commandButton id="acceptAuction" value="Accept auction"  onclick="acceptConfirm.show()"
                                         rendered="#{myArticleBean.selectedArticle.articleState == 'forSale'}"/>
                        <p:commandButton id="cancelAuction" value="Cancel auction" onclick="cancelConfirmation.show()"
                                         rendered="#{myArticleBean.selectedArticle.articleState == 'forSale'}" />
                        <p:commandButton id="reSale" value="Resale" onclick="resaleConfirmation.show()"
                                         rendered="#{myArticleBean.selectedArticle.articleState == 'canceled'}"/>
                    </p:panelGrid>
                </h:form>
            </p:panel>
        </p:panelGrid>

        <h:form>
            <p:confirmDialog id="confirmCancel" message="Are you sure to cancel the sale for #{myArticleBean.selectedArticle.name} ?"  
                             header="Confirmation" severity="alert" widgetVar="cancelConfirmation">  
                <p:commandButton id="confirm" value="Yes Sure" update=":contentGrid,:headerForm:growl" oncomplete="cancelConfirmation.hide()"  
                                 actionListener="#{myArticleBean.cancelArticle()}" />  
                <p:commandButton id="decline" value="Not Yet" onclick="cancelConfirmation.hide()" type="button" />   
            </p:confirmDialog>
        </h:form>

        <h:form>
            <p:confirmDialog id="acceptConfirm" message="Are you sure to accept the auction for #{myArticleBean.selectedArticle.name} ?"  
                             header="Accpetation Confirm" severity="alert" widgetVar="acceptConfirm">  
                <p:commandButton id="confirm3" value="Yes Sure" update=":contentGrid,:headerForm:growl" oncomplete="acceptConfirm.hide()"  
                                 actionListener="#{myArticleBean.acceptAuction()}" />  
                <p:commandButton id="decline3" value="Not Yet" onclick="acceptConfirm.hide()" type="button" />   
            </p:confirmDialog>
        </h:form>


        <h:form>
            <p:confirmDialog  id="confirmResale" message="Are you sure to resale #{myArticleBean.selectedArticle.name} ?"  
                              header="Confirmation" severity="alert" widgetVar="resaleConfirmation"> 
                <p:panelGrid columns="3" >
                    <h:outputText value="New end of the auction" styleClass="outputTextLabel"/>
                    <p:calendar id="newEndAUction" value="#{myArticleBean.endAuction}"  mindate="#{submitArticleBean.minDate}" required="true"/>
                    <p:message for="newEndAUction"/>
                </p:panelGrid>
                <p:commandButton id="confirm2" value="Yes Sure" update=":contentGrid,:headerForm:growl" oncomplete="resaleConfirmation.hide()"  
                                 action="#{myArticleBean.reSaleArticle()}" />  
                <p:commandButton id="decline2" value="Not Yet" onclick="resaleConfirmation.hide()" type="button" />   
            </p:confirmDialog> 
        </h:form>
    </h:body>
</html>